/*
 * @Description: 供应链购物车
 * @Author: Kerwin
 * @Date: 2025-09-15 17:29:41
 * @LastEditTime: 2025-10-16 11:07:47
 * @LastEditors:  Please set LastEditors
 */

import { defineStore } from 'pinia'
//api
import { productApi, moneyApi, userApi, baseApi, enumAll } from '@/api'
//工具
import { moneyUtils } from '@/utils/money'
const gylCarStore = defineStore('gylCar', {
  state: () => ({
    shopGYL_Id: 0, //供应链id
    discount: 0, // 当前用户的优惠券金额
    money: 0, // 当前商品的原价
    highestScoreMoney: 0, // 当前每个商品最高抵扣金额
    allHighestScoreMoney: 0, // 所有商品最高抵扣金额
    shopMode: 4, // 默认供应链模式
    loading: false, // 是否正在加载优惠信息
    Ticket: 0, //最终抵扣的金额
    addressId: 0, //收货地址id
    chooseList: [], //选中的商品
    logisticsList: [] //物流信息
  }),

  getters: {
    // 计算当前可用的优惠券金额
    availableTicket: (state) => {
      if (state.discount > state.highestScoreMoney) {
        state.Ticket = state.highestScoreMoney
        return state.highestScoreMoney
      } else {
        state.Ticket = state.discount
        return state.discount
      }
    },
    // 计算展示价格的工具函数
    displayPrice: (state) => {
      return moneyUtils.subtract(state.money, state.availableTicket)
    }
  },

  actions: {
    // 供应链商店id
    setGYL_SHOP_Id(id: any) {
      switch (Number(id)) {
        case 4:
          this.shopGYL_Id = baseApi.advertising_enum.XFL_GYL_SHOP
          uni.setStorageSync('gylShopId', this.shopGYL_Id)
          return this.shopGYL_Id
        case 6:
          this.shopGYL_Id = baseApi.advertising_enum.CLKJ_GYL_SHOP
          uni.setStorageSync('gylShopId', this.shopGYL_Id)
          return this.shopGYL_Id
        default:
          uni.showToast({ title: '请先清空软件，再进入', icon: 'error' })
          break
      }
    },
    // 设置商品最高抵扣金额
    setDeductible(deductible: any) {
      this.allHighestScoreMoney = deductible
    },
    // 设置商品物流列表
    setLogisticsList(list: any) {
      this.logisticsList = list
    },
    // 设置商品最高抵扣金额
    setChooseList(list: any) {
      this.chooseList = list
    },
    // 设置商品最高抵扣金额
    setAddressId(id: number) {
      this.addressId = id
    },
    // 设置商品最高抵扣金额
    setHighestScoreMoney(amount: number) {
      this.highestScoreMoney = amount
    },

    // 设置店铺模式
    setShopMode(mode: number) {
      this.shopMode = mode
    },

    // 获取优惠券信息
    async getDiscountTicket(shopMode?: number) {
      console.log('shopMode', shopMode)

      // 如果传入了shopMode，则更新状态
      if (shopMode !== undefined) {
        this.shopMode = shopMode
      }

      this.loading = true

      try {
        let discount = 0

        switch (Number(shopMode)) {
          case 1:
            // 花积分模式
            // const { data } = await moneyApi.getMemberSomeWalletMoney({
            //   walletRule: enumAll.wallet_rule_id_enum.花积分钱包
            // })
            // discount.value = data
            // flqTicket.value = 0
            // clTicket.value = 0
            discount = 0
            break

          case 4:
            // 复来券模式
            var { data } = await moneyApi.getMemberTotalFulaiquan()
            discount = data || 0
            break

          case 6:
            // 长来模式
            var { data } = await moneyApi.getMemberSomeWalletMoney({
              walletRule: enumAll.wallet_rule_id_enum.现金抵用券
            })
            console.log('现金抵用券', data)
            discount = data || 0
            break

          default:
            discount = 0
        }

        this.discount = Math.floor(discount)

        return Math.floor(discount)
      } catch (error: any) {
        console.error('获取优惠券失败:', error)
        uni.showToast({
          title: '获取优惠券失败' + error.msg,
          icon: 'none'
        })
        this.discount = 0
        return 0
      } finally {
        this.loading = false
      }
    },

    // 刷新优惠信息
    async refreshDiscount() {
      return await this.getDiscountTicket()
    },

    // 重置优惠信息
    resetDiscount() {
      this.discount = 0
      this.highestScoreMoney = 0
    },
    clearData() {
      this.resetDiscount()
      this.money = 0
      this.Ticket = 0
      this.addressId = 0
      this.chooseList = []
      this.logisticsList = []
    }
  },

  persist: true
})

export default gylCarStore
